package com.nav.tictactoe;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.res.Configuration;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.ImageButton;

public class GameActivity extends Activity {
	private static final char X = 'X', O = 'O';
	private static final int size = 3;
	private Cell cell[][] = new Cell[size][size];
	private char curMark = X;
	private int count = 0;
	
	private class Cell implements View.OnClickListener {
		private char mark;
		public char getMark() {
			return mark;
		}
		private ImageButton button;
		public Cell(int id) {
			button = (ImageButton)findViewById(id);
			button.setOnClickListener(this);
		}
		public void onClick(View v) {
			if(mark != X && mark != O) {
				mark = curMark;
				updateView();
				count++;
				checkGame();
				curMark = curMark == X ? O : X;
			}
		}
		public void updateView() {
			if(mark == X)
				button.setBackgroundResource(R.drawable.ic_x);
			else if(mark == O)
				button.setBackgroundResource(R.drawable.ic_o);
		}
		public void saveInstanceState(Bundle saveState) {
			saveState.putChar("mark_" + button.getId(), mark);
		}
		public void restoreInstanceState(Bundle saveState) {
			mark = saveState.getChar("mark_" + button.getId());
		}
	}
	
	public void checkGame() {
		char winMark = ' ';
		int r, c;
		for(r = 0; r < size; r++) {
			for(c = 0; c < size; c++) {
				if(cell[r][c].getMark() != X && cell[r][c].getMark() != O || cell[r][c].getMark() != cell[r][0].getMark())
					break;
			}
			if(c == size)
				winMark = cell[r][0].getMark();
		}
		for(c = 0; c < size; c++) {
			for(r = 0; r < size; r++) {
				if(cell[r][c].getMark() != X && cell[r][c].getMark() != O || cell[r][c].getMark() != cell[0][c].getMark())
					break;
			}
			if(r == size)
				winMark = cell[0][c].getMark();
		}
		for(r = 0; r < size; r++)
			if(cell[r][r].getMark() != X && cell[r][r].getMark() != O || cell[r][r].getMark() != cell[0][0].getMark())
				break;
		if(r == size)
			winMark = cell[0][0].getMark();
		for(r = 0; r < size; r++)
			if(cell[r][size - r - 1].getMark() != X && cell[r][size - r - 1].getMark() != O || cell[r][size - r - 1].getMark() != cell[0][size - 1].getMark())
				break;
		if(r == size)
			winMark = cell[0][size - 1].getMark();
		if(winMark == X || winMark == O || count >= size * size) {
			AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(this);
			dialogBuilder.setTitle("Game Over");
			dialogBuilder.setNeutralButton("New", new DialogInterface.OnClickListener() {
				public void onClick(DialogInterface dialog, int which) {
					Intent intent = new Intent(getApplicationContext(), GameActivity.class);
					startActivity(intent);
					finish();
				}
			});
			dialogBuilder.setNegativeButton("Exit", new DialogInterface.OnClickListener() {
				public void onClick(DialogInterface dialog, int which) {
					finish();
				}
			});
			if(winMark == X || winMark == O)
				dialogBuilder.setMessage(winMark + " wins!!!");
			else if(count >= size * size)
				dialogBuilder.setMessage("Game is draw");
			AlertDialog alertDialog = dialogBuilder.create();
			alertDialog.show();
		}
	}
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_game);
        cell[0][0] = new Cell(R.id.Cell00);
        cell[0][1] = new Cell(R.id.Cell01);
        cell[0][2] = new Cell(R.id.Cell02);
        cell[1][0] = new Cell(R.id.Cell10);
        cell[1][1] = new Cell(R.id.Cell11);
        cell[1][2] = new Cell(R.id.Cell12);
        cell[2][0] = new Cell(R.id.Cell20);
        cell[2][1] = new Cell(R.id.Cell21);
        cell[2][2] = new Cell(R.id.Cell22);
        if(savedInstanceState != null) {
        	for(int r = 0; r < size; r++) {
        		for(int c = 0; c < size; c++) {
        			cell[r][c].restoreInstanceState(savedInstanceState);
        			cell[r][c].updateView();
        		}
        	}
        	curMark = savedInstanceState.getChar("curMark");
        	count = savedInstanceState.getInt("count");
			checkGame();
        }
    }

    @Override
    protected void onSaveInstanceState(Bundle saveState) {
        super.onSaveInstanceState(saveState);
    	for(int r = 0; r < size; r++) {
    		for(int c = 0; c < size; c++)
    			cell[r][c].saveInstanceState(saveState);
    	}
        saveState.putChar("curMark", curMark);
        saveState.putInt("count", count);
    }

    @Override
    public void onConfigurationChanged(Configuration conf) {
    	for(int r = 0; r < size; r++) {
    		for(int c = 0; c < size; c++)
    			cell[r][c].updateView();
    	}
    }
    
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }
}
